class Location
  attr_reader :name
  attr_reader :queue
  attr_writer :links
  attr_reader :operations
  
  def initialize(name)
    @blocked = false
    @name = name
    @queue = Array.new
    @operations = [[],[],[],[],[],[]]
  end
  
  def service_time(max)
   (rand(max)+1)*60
  end
  
  def receive(message)
    @queue << message
  end
  
  def check_future_messages
    @blocked = false if @operations[0].find{|m| m.is_a? Complete}
    @operations.shift.each do |a|
      if a.is_a?(Complete) 
        @queue.insert(0, a)
      else
        receive(a)
      end
    end
    @operations << []
  end
  
  def future_message(message,time)
    index = time
    while @operations[time].nil?
      @operations << []
      index = index - 1
    end
    @operations[time] << message
  end
  
  def process_next
    process(@queue.shift) if !@blocked
  end
  
  def process(message)
    raise "Unknown Message: #{message}" if message 
  end
  
  #  
  #  def process
  #    # not sure if we only have one queue at each location, i.e. only processing one message a minute?
  #    message = queue.get_first
  #    if !message.nil?
  #      raise "Unknown Message: #{message.inspect}" unless yield(message)
  #    end  
  #  end
  
  def refresh
    @queue = Array.new
  end
  
  def queue_size
    @queue.size.to_s
  end
  
  def to_s
    "Location: #@name (#@blocked)\r\n  Queue: #{@queue.join(', ')}\r\n  Wait:: #{@operations.join(', ')}\r\n\r\n"
  end
end